package com.rr.mymoney.jsf;

import java.io.IOException;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.rr.mymoney.model.User;

//this is for Spring, to instantiate the object
@Controller
// this is for Spring - to know the scope when Spring instantiate the object.
// When the
// object is instantiated by JSF, it is useless
@Scope("session")
// this is useless because the object is instantiated by Spring and
// SpringBeanFacesELResolver is used. It would be used if the object would be
// instantiated by JSF. Without this, the JSF would not instantiate it (if it is
// not declared in faces-config.xml)
@ManagedBean
// this is for JSF - to know the scope when JSF instantiate the object. When the
// object is instantiated by Spring, it is useless
@SessionScoped
public class LoginController extends AbstractController {
    private static final long serialVersionUID = -4237330127283927028L;

    private User loggedUser;

    private String j_username;
    private String j_password;
    private boolean _spring_security_remember_me;

    /**
     * @return the j_username
     */
    public String getJ_username() {
	return j_username;
    }

    /**
     * @param j_username
     *            the j_username to set
     */
    public void setJ_username(String j_username) {
	this.j_username = j_username;
    }

    /**
     * @return the j_password
     */
    public String getJ_password() {
	return j_password;
    }

    /**
     * @param j_password
     *            the j_password to set
     */
    public void setJ_password(String j_password) {
	this.j_password = j_password;
    }

    /**
     * @return the _spring_security_remember_me
     */
    public boolean is_spring_security_remember_me() {
	return _spring_security_remember_me;
    }

    /**
     * @param _spring_security_remember_me
     *            the _spring_security_remember_me to set
     */
    public void set_spring_security_remember_me(boolean _spring_security_remember_me) {
	this._spring_security_remember_me = _spring_security_remember_me;
    }

    // This is the action method called when the user clicks the "login" button
    public String doLogin() throws IOException, ServletException {
	ExternalContext context = FacesContext.getCurrentInstance().getExternalContext();

	RequestDispatcher dispatcher = ((ServletRequest) context.getRequest()).getRequestDispatcher("/j_spring_security_check");

	dispatcher.forward((ServletRequest) context.getRequest(), (ServletResponse) context.getResponse());

	FacesContext.getCurrentInstance().responseComplete();
	// It's OK to return null here because Faces is just going to exit.
	return null;
    }

    // This is the action method called when the user clicks the "login" button
    public String doLogout() throws IOException, ServletException {
	ExternalContext context = FacesContext.getCurrentInstance().getExternalContext();

	RequestDispatcher dispatcher = ((ServletRequest) context.getRequest()).getRequestDispatcher("/j_spring_security_logout");

	dispatcher.forward((ServletRequest) context.getRequest(), (ServletResponse) context.getResponse());

	FacesContext.getCurrentInstance().responseComplete();
	// It's OK to return null here because Faces is just going to exit.
	return null;
    }

    public void redirectToHome() throws IOException {
	if (getLoggedUser() != null) {
	    ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
	    externalContext.redirect(externalContext.encodeActionURL(externalContext.getRequestContextPath() + "/pages/home.xhtml"));
	}
    }

    public String cancelLogin() throws IOException {
	ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
	externalContext.redirect(externalContext.encodeActionURL(externalContext.getRequestContextPath() + "/"));
	return null;
    }

    @Override
    public String toString() {
	StringBuilder builder = new StringBuilder();
	builder.append("LoginController [loggedUser=").append(loggedUser).append(", j_username=").append(j_username)
		.append(", j_password=").append(j_password).append(", _spring_security_remember_me=").append(_spring_security_remember_me)
		.append("]");
	return builder.toString();
    }
}
